/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import model.domain.Status;

/**
 *
 * @author damile
 */
public class StatusConverter implements Converter {
    
    public StatusConverter(){
        
    }


@Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {

        if (value == null || (value.trim().length() == 0))
        {
            return value;
        }

        Status status = new Status();

        String[] splitedString = value.split(" - ");
        status.setStatusID(Integer.parseInt(splitedString[0]));
        status.setDescription(splitedString[1]);

        return status;
    }

    @Override
      public String getAsString(FacesContext context, UIComponent component, Object value)
      {

       Status status = null;

        if (value instanceof Status)
        {
            status = (Status)value;

            StringBuilder statusAsString = new StringBuilder();
            statusAsString.append(status.getStatusID());
            statusAsString.append(" - ");
            statusAsString.append(status.getDescription());
            String string = statusAsString.toString();

            return string;
        }
        return "";
    }


}
